import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/Index.vue'),
    children:[
      {
        path: '/index/home',
        name: 'home',
        meta:{title :'首页'},
        component: () => import('../views/Lndex/Home.vue'),
        
      },
      {
        path:'/index',
        redirect:'/index/home' 
      },     
    ]
  },
  {
    path: '/baobao',
    name: 'baobao',
    component: () => import('../views/BaoBao.vue'),
    children:[
      {
        path: '/baobao/bao',
        name: 'bao',
        meta:{title :'爆爆团'},
        component: () => import('../views/Lndex/Bao.vue')
      },
      {
        path:'/baobao',
        redirect:'/baobao/bao',
      }, 
    ]
  },
  {
    path: '/date',
    name: 'date',
    component: () => import('../views/Date.vue'),
    children:[
      {
        path: '/date/datelist',
        name: 'datelist',
        meta:{title :'订单'},
        component: () => import('../views/Lndex/Datelisk.vue')
      },
      {
        path:"/date",
        redirect:"/date/datelist"
      }
    ]
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import('../views/Mine.vue'),
    children:[
      {
        path: '/mine/my',
        name: 'my',
        meta:{title :'我的'},
        component: () => import('../views/Lndex/My.vue')
      },
      {
        path:"/mine",
        redirect:"/mine/my"
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    meta:{title :'登录'},
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/register',
    name: 'register',
    meta:{title :'注册'},
    component: () => import('../views/Register.vue'),
  },
  {
    path:'/',
    redirect:'/index'
  },
  {
    path:'*',
    component:()=>import('../views/NotFound.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  //动态更新网页标题
 document.title = to.meta.title;

 var token = localStorage.getItem('token');

 if(to.path == '/date/datelist' || to.path == '/mine/my'){
   if(token ){
     next();
   }else{
     next('/login');
   }

 }else{
   next();
 }
})

export default router
